package de.slothsoft.android.authenticator;

import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import de.slothsoft.android.R;
import de.slothsoft.android.authenticator.Authenticator.Registration;
import de.slothsoft.android.authenticator.LoginActivity.LoginOptions;

public class RegisterActivity extends Activity {

    private LoginOptions loginOptions;
    private TextView userId;
    private TextView userPassword;
    private TextView userEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_register);

	this.loginOptions = (LoginOptions) getIntent().getSerializableExtra(LoginActivity.ARG_LOGIN_OPTIONS);
	this.userId = (TextView) findViewById(R.id.register_id);
	this.userPassword = (TextView) findViewById(R.id.register_password);
	this.userEmail = (TextView) findViewById(R.id.register_email);

	hookListeners();
    }

    private void hookListeners() {
	findViewById(R.id.register_alreadyAMember).setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		setResult(RESULT_CANCELED);
		finish();
	    }
	});
	findViewById(R.id.register_submit).setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		createAccount();
	    }
	});
    }

    protected void createAccount() {
	new AsyncTask<String, Void, Intent>() {

	    @Override
	    protected Intent doInBackground(String... params) {
		final Intent res = new Intent();
		res.putExtras(performRegistration());
		return res;
	    }

	    @Override
	    protected void onPostExecute(Intent intent) {
		if (intent.hasExtra(AccountManager.KEY_ERROR_MESSAGE)) {
		    showMessage(intent.getStringExtra(AccountManager.KEY_ERROR_MESSAGE));
		} else {
		    setResult(RESULT_OK, intent);
		    finish();
		}
	    }
	}.execute();
    }

    protected Bundle performRegistration() {
	Log.d("slothsoft", "performRegistration()"); //$NON-NLS-1$ //$NON-NLS-2$
	Bundle data = new Bundle();
	try {
	    Registration registration = new Registration(this.userId.getText().toString(), this.userPassword.getText()
		    .toString(), this.userEmail.getText().toString());
	    if (this.loginOptions.oAuth2Authenticator.registerUser(registration)) {
		data.putSerializable(AccountManager.KEY_ACCOUNT_NAME, registration);
		data.putSerializable(AccountManager.KEY_AUTHTOKEN, AccountAuthenticator.DUMMY_TOKEN);
	    } else {
		data.putString(AccountManager.KEY_ERROR_MESSAGE, "Authentication failed"); //$NON-NLS-1$
	    }

	} catch (NetworkErrorException e) {
	    data.putString(AccountManager.KEY_ERROR_MESSAGE, e.getMessage());
	}
	return data;
    }

    @Override
    public void onBackPressed() {
	setResult(RESULT_CANCELED);
	super.onBackPressed();
    }

    protected void showMessage(String message) {
	Toast toast = Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT);
	toast.show();
    }

}
